package com.blog.boot.custom.function;

import com.blog.boot.api.entity.UserEntity;
import com.blog.boot.webapp.utils.CommonUtils;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.beetl.core.Context;
import org.beetl.core.Function;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : cailong
 * @since : Created in 上午 10:03 2018/12/21 0021
 */
public class ObjTransformationJson implements Function {
    @Override
    public Object call(Object[] objects, Context context) {
        Map<String,Object> result = new HashMap<>();
        if (Objects.isNull(objects[0])){
            result.put("id",-1);
            result.put("username","游客");
            result.put("avatar","/index/images/avatar/00.jpg");
            result.put("jointime","1970-1-1");
            return JSONObject.fromObject(result).toString();
        }
        UserEntity entity = (UserEntity) objects[0];
        result.put("id",entity.getId());
        result.put("username",entity.getUsername());
        result.put("avatar",entity.getAvatar());
        result.put("jointime",entity.getJointime());
        return JSONObject.fromObject(result).toString();
    }
}
